package com.hubu.parking.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hubu.parking.entity.park.Notice;
import com.hubu.parking.entity.park.User;
import com.hubu.parking.mapper.NoticeMapper;
import com.hubu.parking.model.dto.park.PageListNoticeRequest;
import com.hubu.parking.model.vo.NoticeVo;
import com.hubu.parking.service.NoticeService;
import com.hubu.parking.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 19468
 * @description 针对表【notice(停车场公告信息表)】的数据库操作Service实现
 * @createDate 2025-03-02 16:52:08
 */
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements NoticeService {

    @Resource
    private UserService userService;

    @Override
    public Page<NoticeVo> pageListNoticeRecord(PageListNoticeRequest pageRequest) {
        // 1.分页参数
        Page<Notice> page = new Page<>();
        page.setCurrent(pageRequest.getCurrent());
        page.setSize(pageRequest.getPageSize());
        // 2.查询条件
        LambdaQueryWrapper<Notice> queryWrapper = new LambdaQueryWrapper<>();
        // 2.1 用户id
        Integer userId = pageRequest.getCreatedBy();
        if (userId != null) {
            queryWrapper.eq(Notice::getCreatedBy, userId);
        }
        // 2.2 标题
        String title = pageRequest.getTitle();
        if (StrUtil.isNotBlank(title)) {
            queryWrapper.like(Notice::getTitle, title);
        }
        // 2.3 内容
        String content = pageRequest.getContent();
        if (StrUtil.isNotBlank(content)) {
            queryWrapper.like(Notice::getContent, content);
        }
        // 3.取出用户姓名和车位编号设置进去
        Page<Notice> sourcePage = this.page(page, queryWrapper);
        List<Notice> records = sourcePage.getRecords();
        ArrayList<NoticeVo> noticeVos = new ArrayList<>();
        for (Notice record : records) {
            NoticeVo noticeVo = new NoticeVo();
            BeanUtil.copyProperties(record, noticeVo);
            // 设置用户信息
            User userById = userService.getById(record.getCreatedBy());
            if (userById != null) {
                noticeVo.setUserName(userById.getUserName());
                noticeVo.setUserAccount(userById.getUserAccount());
            }
            // 设置完后，添加
            noticeVos.add(noticeVo);
        }
        // 4.重新填充records
        Page<NoticeVo> resultPage = new Page<>();
        BeanUtil.copyProperties(sourcePage, resultPage);
        resultPage.setRecords(noticeVos);
        return resultPage;
    }

    @Override
    public List<Notice> listNoticeNoPage() {
        return this.list(new LambdaQueryWrapper<Notice>().orderByDesc(Notice::getUpdatedTime));
    }
}




